#!/bin/python3
# -*- coding: utf-8 -*-

import carla
from base import AgentBase
from carla_rl.sensor import AroundviewFisheyeCamera4
import time
import random

class Car(AgentBase):
    def __init__(self, name: str,
                 agent_type: str,
                 world: carla.World,
                 record: bool = False,
                 record_root: str = '',
                 spawn_transform: carla.Transform = None,
                 aroundview_fisheye_camera4_configs: dict = {}
                ):
        super().__init__(name, agent_type, world, spawn_transform)
        self.aroundview_fisheye_camera4_configs = aroundview_fisheye_camera4_configs
        
        # 4个鱼眼相机
        self.aroundview_fisheye_camera4 = None
        self.agent = None
        self.record = record
        self.record_root = record_root

    def _random_generate(self, seed=None):
        self.agent = super().random_generate(seed)
        return self.agent

    def _generate_fisheye_sensor(self, record, record_root):
        self.aroundview_fisheye_camera4 = AroundviewFisheyeCamera4(record, record_root,
            self.agent, self.aroundview_fisheye_camera4_configs)
        self.aroundview_fisheye_camera4.generate()
        
    
    def generate(self):
        self._random_generate()
        self._generate_fisheye_sensor(self.record, self.record_root)
        self.update_spector()
        print("self.agent: {}", self.agent)
        self.agent.set_autopilot(True)
    
    def destroy(self):
        super().destroy()
        if self.aroundview_fisheye_camera4 is not None:
            self.aroundview_fisheye_camera4.destroy()
    
    def update_spector(self):
        self.spector = self.world.get_spectator()
        transform = self.agent.get_transform()
        self.spector.set_transform(carla.Transform(transform.location + carla.Location(z=20),
            carla.Rotation(pitch=-90)))


if __name__ == '__main__':
    from carla_rl.environment import CarlaEnv
    env = CarlaEnv()
    env.reset()
    env.set_sync_mode()

    aroundview_fisheye_camera4_configs = {
        "aroundview_front": {
            "image_size_x": "480",
            "image_size_y": "360",
            "fov": "150",
        },
        "aroundview_right": {
            "image_size_x": "480",
            "image_size_y": "360",
            "fov": "150",
        },
        "aroundview_back": {
            "image_size_x": "480",
            "image_size_y": "360",
            "fov": "150",
        },
        "aroundview_left": {
            "image_size_x": "480",
            "image_size_y": "360",
            "fov": "150",
        }
    }
    agent = Car("agent01", "vehicle*", env.world,
        aroundview_fisheye_camera4_configs=aroundview_fisheye_camera4_configs)
    agent.generate()

    import pygame
    pygame.init()
    pygame.font.init()
    display = pygame.display.set_mode((1920, 1080), pygame.HWSURFACE | pygame.DOUBLEBUF)
    
    while True:
        agent.world.tick()
        agent.update_spector()
        agent.aroundview_fisheye_camera4.vis_frame(display)
